package com.hd.controller.commons;

import com.hd.entity.Attach;
import com.hd.service.AttachService;
import com.hd.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;

@Controller
@RequestMapping("/pdfView")
public class PdfViewController {
    @Autowired
    private AttachService attachService;
    @RequestMapping("view")
    public String view(HttpServletRequest req, HttpServletResponse resp) {
        // 获取name参数
        String id=req.getParameter("name");
        Attach attachFile = attachService.get(id);
        File newfile = new File(Const.BASE_UPLOAD_PATH+attachFile.getPath());
        byte[] data = null;
        try {
            FileInputStream input = new FileInputStream(newfile);
            data = new byte[input.available()];
            input.read(data);
            resp.getOutputStream().write(data);
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
